cmake_minimum_required(VERSION 3.0)

project(media-downloader)

add_definitions(-Wextra -Wall -pedantic -std=c++14)

project(media-downloader)

set( PGR_VERSION "1.0.0" )
set( COPYRIGHT_YEARS "2021" )

INCLUDE(GNUInstallDirs)

set(CMAKE_CXX_STANDARD 14)

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(Qt5 COMPONENTS Core Widgets REQUIRED)

message(STATUS "Found Qt5Widgets, version  ${Qt5Widgets_VERSION}")
message(STATUS "Found Qt5Core, version  ${Qt5Core_VERSION}")

add_definitions(${Qt5Widgets_DEFINITIONS})
add_definitions(${Qt5Core_DEFINITIONS})
include_directories(${PROJECT_BINARY_DIR})

# uninstall target
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)

add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)

set(UI_FILES
    src/mainwindow.ui)

set(MOC_FILES
    src/mainwindow.h
    src/basicdownloader.h
    src/batchdownloader.h
    src/batchfiledownloader.h
    src/configure.h
    src/playlistdownloader.h)

set(SRC
    src/mainwindow.cpp
    src/main.cpp
    src/settings.cpp
    src/utility.cpp
    src/basicdownloader.cpp
    src/about.cpp
    src/batchdownloader.cpp
    src/tabmanager.cpp
    src/batchfiledownloader.cpp
    src/configure.cpp
    src/playlistdownloader.cpp)

set(RESOUCES
    src/icon.qrc)

QT5_WRAP_UI(UI ${UI_FILES})
QT5_WRAP_CPP(MOC ${MOC_FILES})
QT5_ADD_RESOURCES(TRAY_RC_SRCS ${RESOUCES})

if(WIN32)
    add_executable(media-downloader WIN32 ${MOC} ${UI} ${SRC} ${TRAY_RC_SRCS})
    set_target_properties(media-downloader PROPERTIES COMPILE_FLAGS "-Wextra -Wall -s -pedantic")
else()
    add_executable(media-downloader ${MOC} ${UI} ${SRC} ${TRAY_RC_SRCS})
    set_target_properties(media-downloader PROPERTIES COMPILE_FLAGS "-Wextra -Wall -s -fPIC -pedantic")
endif()

target_link_libraries(media-downloader ${Qt5Widgets_LIBRARIES} ${Qt5Core_LIBRARIES})

install(TARGETS media-downloader RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

install(FILES src/media-downloader.desktop
    DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications)

install(FILES src/media-downloader.png
    DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pixmaps)

file( WRITE ${PROJECT_BINARY_DIR}/version.h
"
#define VERSION \"${PGR_VERSION}\"
#define COPYRIGHT \"${COPYRIGHT_YEARS}\"
")

if( WIN32 )

    find_path( libstdc_PATH libstdc++-6.dll )

    find_path( gcc_PATH gcc.exe )

    find_path( windeployqt_PATH windeployqt.exe PATHS "${CMAKE_PREFIX_PATH}/bin" )

file( WRITE ${PROJECT_BINARY_DIR}/media-downloader_installer_commands.txt
"
set PATH=${gcc_PATH};${libstdc_PATH};%PATH%\r\n
${windeployqt_PATH}/windeployqt --release media-downloader.exe\r\n
")

file( WRITE ${PROJECT_BINARY_DIR}/media-downloader_windows_installer.iss
"
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName \"Media Downloader\"
#define MyAppVersion \"${PGR_VERSION}\"
#define MyAppPublisher \"Francis Banyikwa\"
#define MyAppURL \"https://github.com/mhogomchungu/media-downloader\"
#define MyAppExeName \"media-downloader.exe\"
#define MyLibrariesLocation \"C:\\projects\\media-downloader.libraries.Qt-${Qt5Core_VERSION}\\\"
#define MyEXELocation \"${PROJECT_BINARY_DIR}\\media-downloader.exe\"
#define MyOutPutPath  \"C:\\projects\\media-downloader.installer\\Qt-${Qt5Core_VERSION}\"
#define MySourcesPath \"C:\\projects\\media-downloader\\\"

[Setup]
AppId={{6DD595EF-ECA2-481B-B008-CB7302603A0D}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\\{#MyAppName}
LicenseFile={#MySourcesPath}\\LICENSE.txt
OutputDir={#MyOutPutPath}
SetupIconFile={#MySourcesPath}\\src\\media-downloader.ico
Compression=lzma
SolidCompression=yes
DisableProgramGroupPage=yes
OutputBaseFilename=MediaDownloader-${PGR_VERSION}.setup

[Tasks]
Name: \"desktopicon\"; Description: \"{cm:CreateDesktopIcon}\"; GroupDescription: \"{cm:AdditionalIcons}\"; Flags: unchecked
Name: \"quicklaunchicon\"; Description: \"{cm:CreateQuickLaunchIcon}\"; GroupDescription: \"{cm:AdditionalIcons}\"; Flags: unchecked; OnlyBelowVersion: 0,6.1

[Files]
Source: {#MyEXELocation}; DestDir: {app}; Flags: ignoreversion

Source: {#MyLibrariesLocation}\\*.dll; DestDir: {app} ; Flags: ignoreversion

Source: {#MySourcesPath}\\LICENSE.txt; DestDir: {app}; Flags: ignoreversion

Source: {#MyLibrariesLocation}\\bearer\\* ; DestDir: {app}\\bearer; Flags: ignoreversion

Source: {#MyLibrariesLocation}\\iconengines\\* ; DestDir: {app}\\iconengines; Flags: ignoreversion

Source: {#MyLibrariesLocation}\\imageformats\\*; DestDir: {app}\\imageformats; Flags: ignoreversion

Source: {#MyLibrariesLocation}\\styles\\*; DestDir: {app}\\styles; Flags: ignoreversion

Source: {#MyLibrariesLocation}\\platforms\\*; DestDir: {app}\\platforms; Flags: ignoreversion

; Source: {#MyLibrariesLocation}\\translations\\*; DestDir: {app}\\translations; Flags: ignoreversion

; Source: {#MySourcesPath}\\translations\\*.qm; DestDir: {app}\\translations; Flags: ignoreversion

Source: {#MySourcesPath}\\src\\media-downloader.ico; DestDir: {app}\\icons; Flags: ignoreversion

; NOTE: Don't use \"Flags: ignoreversion\" on any shared system files

[Icons]
Name: \"{commonprograms}\\{#MyAppName}\"; Filename: \"{app}\\{#MyAppExeName}\"; IconFilename: \"{app}\\icons\\media-downloader.ico\"; WorkingDir: \"{app}\"
;Name: \"{commonprograms}\\Uninstall {#MyAppName}\"; Filename: \"{uninstallexe}\"; IconFilename: \"{app}\\icons\\media-downloader.ico\"; WorkingDir: \"{app}\"

Name: \"{commondesktop}\\{#MyAppName}\"; Filename: \"{app}\\{#MyAppExeName}\"; IconFilename: \"{app}\\icons\\media-downloader.ico\"; WorkingDir: \"{app}\"; Tasks: desktopicon
Name: \"{userappdata}\\Microsoft\\Internet Explorer\\Quick Launch\\{#MyAppName}\"; Filename: \"{app}\\{#MyAppExeName}\"; IconFilename: \"{app}\\icons\\media-downloade.ico\"; WorkingDir: \"{app}\"; Tasks: quicklaunchicon

[Run]
Filename: \"{app}\\{#MyAppExeName}\"; Description: \"{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}\"; Flags: nowait postinstall skipifsilent

[Languages]
Name: \"english\"; MessagesFile: \"compiler:Default.isl\"
Name: \"brazilianportuguese\"; MessagesFile: \"compiler:Languages\\BrazilianPortuguese.isl\"
Name: \"catalan\"; MessagesFile: \"compiler:Languages\\Catalan.isl\"
Name: \"corsican\"; MessagesFile: \"compiler:Languages\\Corsican.isl\"
Name: \"czech\"; MessagesFile: \"compiler:Languages\\Czech.isl\"
Name: \"danish\"; MessagesFile: \"compiler:Languages\\Danish.isl\"
Name: \"dutch\"; MessagesFile: \"compiler:Languages\\Dutch.isl\"
Name: \"finnish\"; MessagesFile: \"compiler:Languages\\Finnish.isl\"
Name: \"french\"; MessagesFile: \"compiler:Languages\\French.isl\"
Name: \"german\"; MessagesFile: \"compiler:Languages\\German.isl\"
Name: \"hebrew\"; MessagesFile: \"compiler:Languages\\Hebrew.isl\"
Name: \"italian\"; MessagesFile: \"compiler:Languages\\Italian.isl\"
Name: \"japanese\"; MessagesFile: \"compiler:Languages\\Japanese.isl\"
Name: \"norwegian\"; MessagesFile: \"compiler:Languages\\Norwegian.isl\"
Name: \"polish\"; MessagesFile: \"compiler:Languages\\Polish.isl\"
Name: \"portuguese\"; MessagesFile: \"compiler:Languages\\Portuguese.isl\"
Name: \"russian\"; MessagesFile: \"compiler:Languages\\Russian.isl\"
Name: \"slovenian\"; MessagesFile: \"compiler:Languages\\Slovenian.isl\"
Name: \"spanish\"; MessagesFile: \"compiler:Languages\\Spanish.isl\"
Name: \"turkish\"; MessagesFile: \"compiler:Languages\\Turkish.isl\"
Name: \"ukrainian\"; MessagesFile: \"compiler:Languages\\Ukrainian.isl\"
")

endif()
